package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.service.IDeptFileArchivedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkSummaryMapper;
import com.ruoyi.system.domain.WorkSummary;
import com.ruoyi.system.service.IWorkSummaryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 工作总结Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class WorkSummaryServiceImpl implements IWorkSummaryService
{
    @Autowired
    private WorkSummaryMapper workSummaryMapper;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;

    /**
     * 查询工作总结
     *
     * @param id 工作总结主键
     * @return 工作总结
     */
    @Override
    public WorkSummary selectWorkSummaryById(Long id)
    {

        WorkSummary workSummary = workSummaryMapper.selectWorkSummaryById(id);
        //会议记录文件
        String fileUrl = workSummary.getFileUrl();
        if(StringUtils.isNotBlank(fileUrl)){
            List<String> fileUrlList = Arrays.asList(fileUrl.split(","));
            workSummary.setFileUrlList(fileUrlList);
        }
        return workSummary;
    }

    /**
     * 查询工作总结列表
     *
     * @param workSummary 工作总结
     * @return 工作总结
     */
    @Override
    public List<WorkSummary> selectWorkSummaryList(WorkSummary workSummary)
    {
        return workSummaryMapper.selectWorkSummaryList(workSummary);
    }

    /**
     * 新增工作总结
     *
     * @param workSummary 工作总结
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWorkSummary(WorkSummary workSummary)
    {
        workSummary.setCreateTime(DateUtils.getNowDate());
        //附件
        List<String> fileUrlList = workSummary.getFileUrlList();
        String fileUrl = StringUtils.join(fileUrlList, ",");
        workSummary.setFileUrl(fileUrl);
        int i = workSummaryMapper.insertWorkSummary(workSummary);
        //查询用户之前是否创建过工作总结文件夹
        DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
        deptFileArchivedVo.setCreateBy(String.valueOf(workSummary.getUserId()));
        deptFileArchivedVo.setParentId(5L);
        List<DeptFileArchived> deptFileArchiveds = deptFileArchivedService.selectDeptFileArchivedList(deptFileArchivedVo);
        Long archivedId = 0L;
        if(CollectionUtils.isEmpty(deptFileArchiveds)){
            //创建工作总结文件夹
            DeptFileArchived deptFileArchived = new DeptFileArchived();
            deptFileArchived.setDeptId(workSummary.getDeptId());
            deptFileArchived.setCreateBy(String.valueOf(workSummary.getUserId()));
            deptFileArchived.setCreateName(workSummary.getUserName());
            deptFileArchived.setFileType("0");
            deptFileArchived.setFileName(workSummary.getUserName());
            deptFileArchived.setParentId(5L);
            deptFileArchived.setAssociationId(String.valueOf(workSummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchived);
            archivedId = deptFileArchived.getArchivedId();
        }else{
            DeptFileArchived deptFileArchived = deptFileArchiveds.get(0);
            archivedId = deptFileArchived.getArchivedId();
        }
        //创建总结主题文件夹
        DeptFileArchived deptFileArchived1 = new DeptFileArchived();
        deptFileArchived1.setDeptId(workSummary.getDeptId());
        deptFileArchived1.setCreateBy(String.valueOf(workSummary.getUserId()));
        deptFileArchived1.setCreateName(workSummary.getUserName());
        deptFileArchived1.setFileType("0");
        deptFileArchived1.setFileName(workSummary.getSummaryTheme());
        deptFileArchived1.setParentId(archivedId);
        deptFileArchived1.setAssociationId(String.valueOf(workSummary.getId()));
        deptFileArchivedService.insertDeptFileArchived(deptFileArchived1);
        //创建文件
        int num = 1;
        for(String file : fileUrlList){
            DeptFileArchived deptFileArchived2 = new DeptFileArchived();
            deptFileArchived2.setDeptId(workSummary.getDeptId());
            deptFileArchived2.setCreateBy(String.valueOf(workSummary.getUserId()));
            deptFileArchived2.setCreateName(workSummary.getUserName());
            deptFileArchived2.setFileType("1");
            deptFileArchived2.setFileName("附件"+num);
            deptFileArchived2.setParentId(deptFileArchived1.getArchivedId());
            deptFileArchived2.setAssociationId(String.valueOf(workSummary.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchived2);
            num++;
        }
        return i;
    }

    /**
     * 修改工作总结
     *
     * @param workSummary 工作总结
     * @return 结果
     */
    @Override
    public int updateWorkSummary(WorkSummary workSummary)
    {
        return workSummaryMapper.updateWorkSummary(workSummary);
    }

    /**
     * 删除工作总结信息
     *
     * @param id 工作总结主键
     * @return 结果
     */
    @Override
    public int deleteWorkSummaryById(Long id)
    {
        return workSummaryMapper.deleteWorkSummaryById(id);
    }
}
